﻿using Nop.Core.Domain.Directory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nop.Core.Domain.Logistics
{
    /// <summary>
    /// Represents a zone. Identified by the postal code.
    /// </summary>
    public class Zone : BaseEntity
    {
        /// <summary>
        /// Gets or sets the district identifier
        /// </summary>
        public virtual int DistrictId { get; set; }

        /// <summary>
        /// Gets or sets the zone group identifier
        /// </summary>
        public virtual int? ZoneGroupId { get; set; }

        /// <summary>
        /// Zone name
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        /// The zip or postal code parts assocaited with zones
        /// </summary>
        public virtual string ZipPostalCode { get; set; }
       
        /// <summary>
        /// Gets or sets the district
        /// </summary>
        public virtual District District { get; set; }

        /// <summary>
        /// Gets or sets the zone group
        /// </summary>
        public virtual ZoneGroup ZoneGroup { get; set; }
    }

    public static class ZoneExtensions
    {
        private static char ZipPostalCodeSeparator = '|';

        public static string[] GetZipPostalCodeArray(this Zone zone)
        {
            return zone.ZipPostalCode.Split(ZipPostalCodeSeparator).Select(x => x.Trim()).ToArray();
        }

        public static bool IsZipPostalCodeInZone(this Zone zone, string zipPostalCode)
        {
            bool res = false;
            string cc = zone.District.CountryCode.ToUpperInvariant();
            switch (cc)
            {
                case "CA":
                    res = zone.GetZipPostalCodeArray().Contains(zipPostalCode.Trim().Substring(0, 3));
                    break;
                default:
                    throw new NopException("The country code ({0}) is invalid.", cc);
            }
            return res;
        }
    }
}
